/* 
* DisplayHandler.h
*
* Created: 10/4/2013 7:39:00 PM
* Author: brodzki
*/

#ifndef DISPLAYHANDLER_H_
#define DISPLAYHANDLER_H_

#include "Register.h"

// config

struct DisplayConfig
{
	Register rs_ddr;
	Register rs_port;
	unsigned int rs_bit;
	
	Register e_ddr;
	Register e_port;
	unsigned int e_bit;
	
	Register db4_ddr;
	Register db4_port;
	unsigned int db4_bit;
	
	Register db5_ddr;
	Register db5_port;
	unsigned int db5_bit;

	Register db6_ddr;
	Register db6_port;
	unsigned int db6_bit;

	Register db7_ddr;
	Register db7_port;
	unsigned int db7_bit;				
};

// instructions

namespace Commands
{
	const short clear					= 0x01;

	const short home					= 0x02;

	const short entry_mode				= 0x04;
	const short em_shift_cursor			= 0;
	const short em_shift_display		= 1;
	const short em_decrement			= 0;
	const short em_increment			= 2;

	const short display_on_off			= 0x08;
	const short display_off				= 0;
	const short display_on				= 4;
	const short cursor_off				= 0;
	const short cursor_on				= 2;
	const short cursor_noblink			= 0;
	const short cursor_blink			= 1;

	const short display_cursor_shift	= 0x10;
	const short shift_cursor			= 0;
	const short shift_display			= 8;
	const short shift_left				= 0;
	const short shift_right				= 4;

	const short function_set			= 0x20;
	const short font5x7					= 0;
	const short font5x10				= 4;
	const short one_line				= 0;
	const short two_line				= 8;
	const short four_bit				= 0;
	const short eight_bit				= 16;

	const short cgram_set				= 0x40;

	const short ddram_set				= 0x80;
}

class DisplayHandler
{
	DisplayConfig config_;
	
	void out_nibble(unsigned char);
	void write(unsigned char);
	void initialize();
public:	
	DisplayHandler(DisplayConfig config) : config_(config) { initialize(); }
	void write_command(unsigned char);
	void write_data(unsigned char);
	void write_text(char *);
	void go_to(unsigned char, unsigned char);
	void clear();
	void home();
};

#endif // DISPLAYHANDLER_H
